<?php

$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
set_include_path(get_include_path().PATH_SEPARATOR.$baseDir.PATH_SEPARATOR.$baseDir.'/incubator');

include '../../testingfunctions.php';
include 'Pone/Pone_Exception.php';
include 'Pone/Database/Pone_Database_ConnectionFactory.php';
include_once './dbconfig.php'; // $config is defined here
include_once './test_setup.php'; // refreshTestData() is defined here

try
{
    refreshTestData($config);
    // /home/web/windows.php.net/docroot/downloads/snaps/
    $conn = Pone_Database_ConnectionFactory::getConnection($config);
    $stmt = $conn->createStatement();

    // Test 1
    echo '<h4>Test 1</h4>';
    $stmt->execute('SELECT 1');
    $rs   = $stmt->getResultSet();
    $dataSet = $rs->getAssociativeArray();
    assertTrue('1' === $dataSet[0][1], 'Expected: The array returned contains 1 row and 1 column. It contains a field named 1 and that field\'s value is "1" (a string)');
    $rs->close();
}
catch (Pone_Exception_DatabaseAccess $ex)
{
    echo implode('. ', $ex->getTraceMessages());
    echo '<br />';
    echo $ex->getNativeMessage();
}
catch (Exception $ex)
{
    echo $ex->getMessage();
}

try
{
    // Test 2
    echo '<h4>Test 2</h4>';
    $stmt->execute('SELECT * FROM quotes');
    $rs   = $stmt->getResultSet();
    assertTrue(array() === $rs->getAssociativeArray(), 'Expected: empty array');
    $rs->close();
}
catch (Pone_Exception_DatabaseAccess $ex)
{
    echo implode('. ', $ex->getTraceMessages());
    echo '<br />';
    echo $ex->getNativeMessage();
}
catch (Exception $ex)
{
    echo $ex->getMessage();
}

try
{
    // Test 3
    echo '<h4>Test 3</h4>';
    $stmt->execute('SELECT * FROM quotes');
    $rs   = $stmt->getResultSet();
    assertTrue(0 === $rs->rowCount(), 'Expected: Row count is 0');
    $rs->close();
}
catch (Pone_Exception_DatabaseAccess $ex)
{
    echo implode('. ', $ex->getTraceMessages());
    echo '<br />';
    echo $ex->getNativeMessage();
}
catch (Exception $ex)
{
    echo $ex->getMessage();
}

try
{
    // Test 4
    echo '<h4>Test 4</h4>';
    $stmt->execute('SELECT PRODUCT_ID FROM product_history_prices');
    $rs   = $stmt->getResultSet();
    assertTrue(9 === $rs->rowCount(), 'Expected: Row count is 9');
    $rs->close();

}
catch (Pone_Exception_DatabaseAccess $ex)
{
    echo implode('. ', $ex->getTraceMessages());
    echo '<br />';
    echo $ex->getNativeMessage();
}
catch (Exception $ex)
{
    echo $ex->getMessage();
}

try
{
    // Test 5
    echo '<h4>Test 5</h4>';
    $stmt->execute('SELECT * FROM product_history_prices');
    $rs   = $stmt->getResultSet();
    assertTrue(9 === $rs->rowCount(), 'Expected: Row count is 9');
    $rs->close();
}
catch (Pone_Exception_DatabaseAccess $ex)
{
    echo implode('. ', $ex->getTraceMessages());
    echo '<br />';
    echo $ex->getNativeMessage();
}
catch (Exception $ex)
{
    echo $ex->getMessage();
}

// Free resources
$stmt->close();
$conn->close();
?>